package com.xoa.util.cms.directive;

import com.xoa.dao.portalColumn.PortalColumnMapper;
import com.xoa.dao.portalTemplate.PortalTemplateMapper;
import com.xoa.dao.cms.SiteMapper;
import com.xoa.model.portalColumn.PortalColumn;
import com.xoa.model.portalColumn.PortalColumnExample;
import com.xoa.model.portalTemplate.PortalTemplateWithBLOBs;
import com.xoa.model.cms.Site;
import com.xoa.util.common.StringUtils;
import freemarker.core.Environment;
import freemarker.template.*;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 获取栏目列表
 */

public class CMSChannelsDirective implements TemplateDirectiveModel {
    @Override
    public void execute(Environment environment, Map map, TemplateModel[] templateModels, TemplateDirectiveBody templateDirectiveBody) throws TemplateException, IOException {

        /**
         * 类别
         * TOP 查询该站点下的所有顶层栏目
         * CHILD 获取当前站点下指定栏目的子栏目
         * IDS 指定栏目列表下的子栏目
         */
        String type = StringUtils.getString(map.get("type"));
        String chnlIdentity = StringUtils.getString(map.get("id"));
        String ids = StringUtils.getString(map.get("ids"));
        int top = StringUtils.getInteger(map.get("top"),0);

        int siteId = StringUtils.getInteger(environment.getCustomAttribute("SITE_ID"),0);
        int channelId = StringUtils.getInteger(environment.getCustomAttribute("channelId"),0);

        Writer w = environment.getOut();
        //是否是预览模式
        boolean preview = (boolean)environment.getCustomAttribute("preview");

        //获取Bean
        //站点信息
        SiteMapper siteMapper = (SiteMapper) environment.getCustomAttribute("siteMapper");
        //栏目
        PortalColumnMapper portalColumnMapper = (PortalColumnMapper) environment.getCustomAttribute("portalColumnMapper");
        //栏目模板信息
        PortalTemplateMapper portalTemplateMapper = (PortalTemplateMapper) environment.getCustomAttribute("portalTemplateMapper");


        //获取站点信息
        Site site = siteMapper.selectByPrimaryKey(siteId);
        List<PortalColumn> portalColumnList = new ArrayList<>();

        if("TOP".equalsIgnoreCase(type)){
            //查询站点的顶级栏目
            PortalColumnExample example = new PortalColumnExample();
            PortalColumnExample.Criteria criteria = example.createCriteria();
            criteria.andPortalIdEqualTo(siteId);
            criteria.andParentColumnIdEqualTo(0);
            portalColumnList = portalColumnMapper.selectByExampleWithBLOBs(example);
        }else if("CHILD".equalsIgnoreCase(type)){
            //获取指定栏目的子栏目
            if("".equals(chnlIdentity)){
                //如果没有指定栏目id，则默认为当前栏目
                PortalColumnExample example = new PortalColumnExample();
                PortalColumnExample.Criteria criteria = example.createCriteria();
                criteria.andPortalIdEqualTo(siteId);
                criteria.andParentColumnIdEqualTo(channelId);
                portalColumnList = portalColumnMapper.selectByExampleWithBLOBs(example);
            }else{
                //栏目标识不为空
                PortalColumnExample example = new PortalColumnExample();
                PortalColumnExample.Criteria criteria = example.createCriteria();
                criteria.andColumnMarkEqualTo(chnlIdentity);
                criteria.andPortalIdEqualTo(siteId);
                List<PortalColumn> portalColumns = portalColumnMapper.selectByExampleWithBLOBs(example);

                if( portalColumns.size()==0){
                    return ;
                }

                example = new PortalColumnExample();

                PortalColumnExample.Criteria criteria1 = example.createCriteria();
                criteria1.andPortalIdEqualTo(siteId);
                criteria1.andParentColumnIdEqualTo(portalColumns.get(0).getColumnId());
                portalColumnList = portalColumnMapper.selectByExampleWithBLOBs(example);

            }
        }else if("IDS".equalsIgnoreCase(type)){


        }



        //
        Object loopObj = environment.getCustomAttribute("loopObj");
        TemplateModel this0 = environment.getVariable("this");

        if(portalColumnList == null ){
            return ;
        }

        int index = 0;
        for(PortalColumn column : portalColumnList){
            PortalTemplateWithBLOBs tm = portalTemplateMapper.selectByPrimaryKey(column.getDetailTemplate());
            Map param = new HashMap();
            //栏目id
            param.put("columnId",column.getColumnId());
            param.put("siteId",column.getSortNo());
            param.put("columnMark",column.getColumnMark());
            param.put("columnName",column.getColumnName());


            //如果是预览中
            if(preview){
                param.put("ABS_PATH", "/site/preview?siteId="+column.getPortalId()+"&channelId="+column.getColumnId());//绝对路径
                param.put("REL_PATH", "/site/preview?siteId="+column.getPortalId()+"&channelId="+column.getColumnId());//相对路径
            }

            int index0 = 0;
            //内部循环变量
            Map _this = new HashMap();
            _this.put("instance", map);
            _this.put("channelId", column.getColumnId());
            _this.put("index", index0);
            if(index0==portalColumnList.size()-1){
                _this.put("isLast", true);
            }else{
                _this.put("isLast", false);
            }
            environment.setVariable("this", new DefaultObjectWrapper().wrap(_this));
            environment.setCustomAttribute("loopObj", param);
            environment.setCustomAttribute("CHANNEL_ID",column.getColumnId());
            if(templateDirectiveBody!=null){
                templateDirectiveBody.render(w);
            }
            environment.setVariable("this", this0);
            environment.setCustomAttribute("CHANNEL_ID",channelId);
            environment.setCustomAttribute("loopObj", loopObj);
        }

    }
}
